﻿using System;
using System.Activities;
using System.Activities.DurableInstancing;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activities;
using System.ServiceModel.Activities.Activation;
using System.Text;
using System.Threading.Tasks;
using YasharEl.Infrastructure.Diagnostics.Logging;
using YasharEl.Infrastructure.ObjectFactory;

namespace YasharEl.Infrastructure.WorkflowModel.ServiceModel
{
    public class StandartWorkflowServiceHostFactory : WorkflowServiceHostFactory
    {
        private static readonly ILogger<StandartWorkflowServiceHostFactory> Logger =
            DependencyContainer.Resolve<IDependencyResolver>().Resolve<ILogger<StandartWorkflowServiceHostFactory>>();

        protected override WorkflowServiceHost CreateWorkflowServiceHost(Activity activity, Uri[] baseAddresses)
        {
            return base.CreateWorkflowServiceHost(activity, baseAddresses);
        }

        protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
        {
            WorkflowServiceHost workflowServiceHost = base.CreateWorkflowServiceHost(service, baseAddresses);

            workflowServiceHost.Closed += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost closed.");
            };

            workflowServiceHost.Closing += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost closing.");
            };

            workflowServiceHost.Faulted += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost faulted.");
            };

            workflowServiceHost.Opened += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost opened.");

                WorkflowServiceHost host = sender as WorkflowServiceHost;
                if (host != null)
                {
                    SqlWorkflowInstanceStore instanceStore = host.DurableInstancingOptions.InstanceStore as SqlWorkflowInstanceStore;
                    if (instanceStore != null)
                    {
                    }
                }
            };

            workflowServiceHost.Opening += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost opening.");
            };

            workflowServiceHost.UnknownMessageReceived += (sender, e) =>
            {
                Logger.Debug("WorkflowServiceHost unknown message received.");
            };

            return workflowServiceHost;
        }
    }
}
